package matrix;


public class GraphLink
{
	protected final int ID;
	private GraphNode firstNode;
	private GraphNode secondNode;
	public static int nextLinkIndex = 0;
	private double length;
	
	protected GraphLink(int ID, GraphNode firstNode, GraphNode secondNode)
	{
		this.setFirstNode(firstNode);
		this.setSecondNode(secondNode);
		this.ID = ID;
		
		double xx1 = firstNode.getPosition().x() * firstNode.getPosition().x();
		double yy1 = firstNode.getPosition().y() * firstNode.getPosition().y();
		double xx2 = secondNode.getPosition().x() * secondNode.getPosition().x();
		double yy2 = secondNode.getPosition().y() * secondNode.getPosition().y();
	
		this.setLength(Math.sqrt(xx1 + yy1) + Math.sqrt(xx2 + yy2));
	}
	
	public GraphLink(GraphNode firstNode, GraphNode secondNode)
	{
		this(nextLinkIndex++, firstNode, secondNode);
	}
	
	public int getID()
	{
		return this.ID;
	}
	
	public String toCSV()
	{
		String value = "";
		
		value += "link;";
		value += this.getID()+";";
		//value += this.getFirstNode().getID()+";";
		//value += this.getSecondNode().getID() + ";";
		value += this.getLength()+";";
		value += "\n";
		
		return value;
	}
	
	public String toXML()
	{
		String value = "";

		value += "<link id=\""+this.getID()+"\" ";
		//value += "firstNode=\""+this.getFirstNode().getID()+"\" ";
		//value += "secondNode=\""+this.getSecondNode().getID()+"\" ";
		value += "length=\""+this.getLength()+"\" ";
		value += "/>\n";
		
		return value;
	}

	public GraphNode getFirstNode() {
		return firstNode;
	}

	public void setFirstNode(GraphNode firstNode) {
		this.firstNode = firstNode;
	}

	public GraphNode getSecondNode() {
		return secondNode;
	}

	public void setSecondNode(GraphNode secondNode) {
		this.secondNode = secondNode;
	}

	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		this.length = length;
	}
}